package com.zxmr.domain.service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zxmr.CommonInit;
import com.zxmr.db.DBManager;
import com.zxmr.domain.Comment;
import com.zxmr.domain.Customer;
import com.zxmr.domain.CustomerComments;
import com.zxmr.domain.CustomerType;

public class CustomerService {

	DBManager db = CommonInit.getDBManager();
	
	public int addCustomer(Customer customer)
	{
		try {
			int id = (Integer)db.getMapper().insert("customer.add",customer);
			return id;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}
	public Customer queryCustomer(int id)
	{
		try
		{
			return (Customer)db.getMapper().queryForObject("customer.findByID",id);
		}catch(SQLException e){
			e.printStackTrace();
		}
		return null;
	}
	public boolean updateCustomer(Customer customer)
	{
		try {
			db.getMapper().update("customer.update", customer);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	@SuppressWarnings("unchecked")
	public List<Customer> queryAllCustomer(int company)
	{
		try {
			return (List<Customer>)db.getMapper().queryForList("customer.findByCompany", company);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<Customer> queryCustomerPagination(int start,int limit,int company)
	{
		Map<String,Object> para = new HashMap<String,Object>();
		para.put("start", start);
		para.put("limit", limit);
		para.put("company", company);
		
		try {
			return (List<Customer>)db.getMapper().queryForList("customer.findByCompanyPagination", para);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<Customer> queryCustomerPagination(CustomerSearchBean bean)
	{
		try {
			return (List<Customer>)db.getMapper().queryForList("customer.searchPagination", bean);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<Customer> queryCustomerForAnsysis(CustomerSearchBean bean)
	{
		try {
			return (List<Customer>)db.getMapper().queryForList("customer.searchForAnsysis", bean);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public int queryCustomerCount(CustomerSearchBean bean)
	{
		try {
			int id = (Integer)db.getMapper().queryForObject("customer.findCountPagination",bean);
			return id;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}
	public int queryCustomerCount(int company)
	{
		try {
			int id = (Integer)db.getMapper().queryForObject("customer.findCountCompany",company);
			return id;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}
	public boolean addCustomerComment(Comment comment,int customer)
	{
		try {
			db.getMapper().startTransaction();
			long commentId = (Long)db.getMapper().insert("comment.add", comment);
			comment.setId(commentId);
			
			CustomerComments cc = new CustomerComments();
			cc.setComment(commentId);
			cc.setCustomer(customer);
			db.getMapper().insert("customer.addComment", cc);
			db.getMapper().commitTransaction();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();

		}finally{
			try {
				db.getMapper().endTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		
		return false;
	}
	public boolean deleteCustomerComment(CustomerComments cc)
	{
		try {
			db.getMapper().startTransaction();
			db.getMapper().delete("customer.deleteComment", cc);
			db.getMapper().delete("comment.delete", cc.getComment());
			db.getMapper().commitTransaction();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();

		}finally{
			
			try {
				db.getMapper().endTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
	public boolean deleteCustomerComment(long comment)
	{
		try {
			CustomerComments cc = (CustomerComments)db.getMapper().queryForObject("customer.queryCustomerCommentByComment", comment);
			db.getMapper().startTransaction();
			db.getMapper().delete("customer.deleteComment", cc);
			db.getMapper().delete("comment.delete", comment);
			db.getMapper().commitTransaction();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			
		}finally{
			try {
				db.getMapper().endTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
	public Comment queryComment(long id)
	{
		try {
			return (Comment)db.getMapper().queryForObject("comment.findByID", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<Comment> queryAllComments(int customer)
	{
		try {
			return (List<Comment>)db.getMapper().queryForList("customer.queryAllComments", customer);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public CustomerComments queryCustomerComment(int comment)
	{
		try {
			return (CustomerComments)db.getMapper().queryForObject("customer.queryCustomerCommentByComment", comment);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Customer> search(String value, int company)
	{
		CustomerSearchBean search = new CustomerSearchBean();
		search.setCompany(company);
		search.setValue(value);
		
		try {
			return (List<Customer>)db.getMapper().queryForList("customer.search", search);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean addCustomerType(CustomerType type)
	{
		try
		{
			db.getMapper().insert("customer.addCustomerType",type);
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	public boolean deleteCustomerType(int id)
	{
		try
		{
			db.getMapper().delete("customer.deleteCustomerType", id);
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	public boolean updateCustomerType(CustomerType type)
	{
		try
		{
			db.getMapper().delete("customer.updateCustomerType", type);
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	public CustomerType queryCustomerType(int id)
	{
		try
		{
			return (CustomerType)db.getMapper().queryForObject("customer.queryCustomerType", id);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<CustomerType> queryAllCustomerType(int company)
	{
		try
		{
			return (List<CustomerType>)db.getMapper().queryForList("customer.queryAllCustomerType", company);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
}
